package com.zfdang.touchhelper.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.aivin.adovertool.R;
import com.zfdang.touchhelper.service.TouchHelperService;
import com.zfdang.touchhelper.viewmodel.HomeViewModel;

/**
 * @author ly-dengpw
 */
public class HomeFragment extends Fragment {
    private HomeViewModel homeViewModel;
    private Context context;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        context = getContext();
        if (context == null) {
            throw new RuntimeException("context is null ...");
        }

        final Drawable drawableYes = ContextCompat.getDrawable(context, R.drawable.ic_right);
        final Drawable drawableNo = ContextCompat.getDrawable(context, R.drawable.ic_wrong);

        // 监听无障碍服务权限是否开启
        final ImageView imageAccessibilityPermission = root.findViewById(R.id.image_accessibility_permission);
        homeViewModel.mAccessibilityPermission.observe(getViewLifecycleOwner(), aBoolean -> {
            Drawable drawable = aBoolean ? drawableYes : drawableNo;
            imageAccessibilityPermission.setImageDrawable(drawable);
        });

        // 监听电池优化的开启情况
        final ImageView imagePowerPermission = root.findViewById(R.id.image_power_permission);
        homeViewModel.mPowerOptimization.observe(getViewLifecycleOwner(), aBoolean -> {
            Drawable drawable = aBoolean ? drawableYes : drawableNo;
            imagePowerPermission.setImageDrawable(drawable);
        });

        root.findViewById(R.id.button_accessibility_permission).setOnClickListener(v -> {
            // 跳转系统，去开启无障碍服务
            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });

        root.findViewById(R.id.button_power_permission).setOnClickListener(v -> {
            // 跳转系统，去设置电池优化
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
            startActivity(intent);
        });

        checkServiceStatus();
        return root;
    }

    @Override
    public void onResume() {
        checkServiceStatus();
        super.onResume();
    }

    public void checkServiceStatus() {
        // 判断 accessibility 是否开启
        homeViewModel.mAccessibilityPermission.setValue(TouchHelperService.isServiceRunning());
        // 判断 power optimization 是否开启
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
        homeViewModel.mPowerOptimization.setValue(hasIgnored);
    }
}